﻿using System;
using System.IO;
using NUnit.Framework;
using SharePoint.Orm.Metal.Rules;
using SharePoint.Orm.TestHelpers;
using SPGenerated.ContentTypes;
using Microsoft.SharePoint;
using SPGenerated.Lists;

namespace SharePoint.Orm.Metal {
	[TestFixture]
	public class CodeGeneratorTest : TestBase {
		[TestFixtureSetUp]
		public virtual void TestInitialize() {
			base.TestInitialize();
			SiteHelpers.EnsureSite();
			SiteHelpers.RemoveData();
		}

		[Test]
		public void TestGenerate() {
			SiteHelpers.EnsureSite();

			string solutionDir = PathHelpers.GetSolutionDirectory();
			CodeGenerator generator = new CodeGenerator();
			CodeGeneratorConfiguration config = new CodeGeneratorConfiguration {
				Url = SiteHelpers.Url,
				DataContextOutputDirectory = Path.Combine(solutionDir, @"SharePoint.Orm.Test\_generated"),
				ContentTypeOutputDirectory = Path.Combine(solutionDir, @"SharePoint.Orm.Test\_generated\ContentTypes"),
				ListOutputDirectory = Path.Combine(solutionDir, @"SharePoint.Orm.Test\_generated\Lists"),
				NameRewriter = new TestNameRewriter()
			};
			config.AddListRule(new ListExcludeRule("*"));
			config.AddListRule(new ListIncludeRule(SiteHelpers.List1Name));
			config.AddListRule(new ListIncludeRule(SiteHelpers.List2Name));
			config.AddListRule(new ListIncludeRule(SiteHelpers.List3Name));
			config.AddListRule(new ListIncludeRule(SiteHelpers.List4Name));
			config.AddListRule(new ListIncludeRule(SiteHelpers.List5Name));
			config.AddListRule(new ListIncludeRule(SiteHelpers.List6Name));
			generator.Generate(config);
		}

		[Test]
		public void TestGenerateWithFinders() {
			SiteHelpers.EnsureSite();

			string solutionDir = PathHelpers.GetSolutionDirectory();
			CodeGenerator generator = new CodeGenerator();
			CodeGeneratorConfiguration config = new CodeGeneratorConfiguration {
				Url = SiteHelpers.Url,
				DataContextNamespaceName = "SPGeneratedWithFinders",
				ContentTypeNamespaceName = "SPGeneratedWithFinders.ContentTypes",
				ListNamespaceName = "SPGeneratedWithFinders.Lists",
				DataContextOutputDirectory = Path.Combine(solutionDir, @"SharePoint.Orm.Test\_generatedWithFinders"),
				ContentTypeOutputDirectory = Path.Combine(solutionDir, @"SharePoint.Orm.Test\_generatedWithFinders\ContentTypes"),
				ListOutputDirectory = Path.Combine(solutionDir, @"SharePoint.Orm.Test\_generatedWithFinders\Lists"),
				NameRewriter = new TestNameRewriter(),
				ContentTypeTemplate = "res://SharePoint.Orm.Metal.Templates.Finders.ContentType.tt, SharePoint.Orm, Version=1.0.0.0, Culture=neutral, PublicKeyToken=5959a709b4f356a5",
				DataContextTemplate = "res://SharePoint.Orm.Metal.Templates.Finders.DataContext.tt, SharePoint.Orm, Version=1.0.0.0, Culture=neutral, PublicKeyToken=5959a709b4f356a5",
				ListTemplate = "res://SharePoint.Orm.Metal.Templates.Finders.List.tt, SharePoint.Orm, Version=1.0.0.0, Culture=neutral, PublicKeyToken=5959a709b4f356a5"
			};
			config.AddListRule(new ListExcludeRule("*"));
			config.AddListRule(new ListIncludeRule(SiteHelpers.List1Name));
			config.AddListRule(new ListIncludeRule(SiteHelpers.List2Name));
			config.AddListRule(new ListIncludeRule(SiteHelpers.List3Name));
			config.AddListRule(new ListIncludeRule(SiteHelpers.List4Name));
			config.AddListRule(new ListIncludeRule(SiteHelpers.List5Name));
			config.AddListRule(new ListIncludeRule(SiteHelpers.List6Name));
			generator.Generate(config);
		}

		[Test]
		public void TestPropertyTypes() {
			Assert.AreEqual(typeof(string), typeof(List4Item).GetProperty(SiteHelpers.List4FieldText).PropertyType);
			Assert.AreEqual(typeof(DateTime?), typeof(List4Item).GetProperty(SiteHelpers.List4FieldDateTime).PropertyType);
			Assert.AreEqual(typeof(string), typeof(List4Item).GetProperty(SiteHelpers.List4FieldNote).PropertyType);
			Assert.AreEqual(typeof(int?), typeof(List4Item).GetProperty(SiteHelpers.List4FieldInteger).PropertyType);
			Assert.AreEqual(typeof(Guid?), typeof(List4Item).GetProperty(SiteHelpers.List4FieldGuid).PropertyType);
			Assert.AreEqual(typeof(bool?), typeof(List4Item).GetProperty(SiteHelpers.List4FieldBoolean).PropertyType);
			Assert.AreEqual(typeof(string), typeof(List4Item).GetProperty(SiteHelpers.List4FieldChoice).PropertyType);
			Assert.AreEqual(typeof(SPFieldUserValue), typeof(List4Item).GetProperty(SiteHelpers.List4FieldUser).PropertyType);
			Assert.AreEqual(typeof(SPFieldUrlValue), typeof(List4Item).GetProperty(SiteHelpers.List4FieldURL).PropertyType);
			Assert.AreEqual(typeof(decimal?), typeof(List4Item).GetProperty(SiteHelpers.List4FieldNumber).PropertyType);
			Assert.AreEqual(typeof(string), typeof(List4Item).GetProperty(SiteHelpers.List4FieldWorkflowEventType).PropertyType);
			Assert.AreEqual(typeof(List1), typeof(List4Item).GetProperty(SiteHelpers.List4FieldLookup).PropertyType);

			//Assert.AreEqual(typeof(string), typeof(List4Item).GetProperty(SiteHelpers.List4FieldFile).PropertyType);
			//Assert.AreEqual(typeof(string), typeof(List4Item).GetProperty(SiteHelpers.List4FieldComputed).PropertyType);
			//Assert.AreEqual(typeof(string), typeof(List4Item).GetProperty(SiteHelpers.List4FieldInvalid).PropertyType);
			//Assert.AreEqual(typeof(string), typeof(List4Item).GetProperty(SiteHelpers.List4FieldWorkflowStatus).PropertyType);
			//Assert.AreEqual(typeof(string), typeof(List4Item).GetProperty(SiteHelpers.List4FieldAllDayEvent).PropertyType);
			//Assert.AreEqual(typeof(string), typeof(List4Item).GetProperty(SiteHelpers.List4FieldRecurrence).PropertyType);
			//Assert.AreEqual(typeof(string), typeof(List4Item).GetProperty(SiteHelpers.List4FieldCrossProjectLink).PropertyType);
		}
	}
}